//
//  ConstructionEventReportViewController.m
//  Runone
//
//  Created by ekkaLyn on 2017/3/15.
//  Copyright © 2017年 runone. All rights reserved.
//

#import "ConstructionEventReportViewController.h"
#import "UnStartEventView.h"
#import "ChooseTimeView.h"
#import "InProgressConstructPositionView.h"
#import "InProgressPicAndVideoView.h"
#import "ChoosePileNOView.h"
#import "SDAutoLayout.h"
#import "TableOperation.h"
#import "PileInfo.h"
#import "CommonUtilityjudge.h"
#import "SGImagePickerController.h"
#import "SVProgressHUD.h"
#import "BCNetAPIClient.h"
#import "LittleVideoViewController.h"
#import "ROActionSheet.h"
#import "ChooseLineTableViewController.h"
#import "ROImagePickViewController.h"
#import "AJPhotoPickerViewController.h"
#import "AJPhotoBrowserViewController.h"
#import "OtherEventMainLineViewController.h"
#import "HighWayRoadRecordDirectionInfoModel.h"
#import "EventDealList.h"
#import "PhotoesScrollView.h"
#import "DateViewController.h"
#import "ChooseLineTableViewController.h"
#import "AppDelegate.h"
#import "ChooseDirectionView.h"
#import "StationModel.h"
#import "CityDirectionController.h"
#import "StationListPopViewController.h"
#import "RampViewController.h"
#import "RODateHelper.h"
#import "NSDate+ROCaculateTime.h"
#import "RequestGinsengModel.h"
#import "Define.h"
#import "ConstructionEventDetailInfo.h"
#import "MJExtension.h"
#import "ParModel.h"
#import "CommonUtilityjudge.h"
#import "EKDateTimePickerView.h"
#import "NSDate+Extension.h"
#import "UIColor+Addition.h"
#import "OneConst.h"

@interface ConstructionEventReportViewController ()<UIScrollViewDelegate, LittleVideoDelegate,AJPhotoPickerProtocol, AJPhotoBrowserDelegate, UIImagePickerControllerDelegate,UINavigationControllerDelegate, UIAlertViewDelegate, UIPopoverPresentationControllerDelegate>{
    AVPlayer *_player;
    AVPlayerItem *_playItem;
    AVPlayerLayer *_playerLayer;
    AVPlayerLayer *_fullPlayer;
    BOOL _isPlaying;
}
@property (nonatomic, strong) UIScrollView     *scroollView;
@property (nonatomic, strong) UIView           *wrapperView;
@property (nonatomic, strong) UIView           *containerV;
@property (nonatomic, strong) UnStartEventView *constructUnitV; // 施工单位
@property (nonatomic, strong) UnStartEventView *personChargeV;      // 现场负责人
@property (nonatomic, strong) UnStartEventView *contacterV;         // 联系人电话
@property (nonatomic, strong) ChooseTimeView   *beginTimeV;         // 计划开始时间
@property (nonatomic, strong) ChooseTimeView   *endTimeV;           // 计划结束时间
@property (nonatomic, strong) InProgressConstructPositionView * constructPositionV;// 施工地点
@property (nonatomic, strong) ChoosePileNOView *beginPileV;      // 开始桩号
@property (nonatomic, strong) ChoosePileNOView *endPileV;        // 结束桩号
@property (nonatomic, strong) ChooseDirectionView  *directionV;      // 行车方向
@property (nonatomic, strong) UnStartEventView *projectV;        // 施工项目
@property (nonatomic, strong) UnStartEventView *descripV;        // 施工描述
@property (nonatomic, strong) InProgressPicAndVideoView *picAndVideoV; // 图片视频按钮
@property(nonatomic, strong) LittleVideoViewController *littleVideoVC;//拍摄小视频界面
@property (nonatomic, strong) PhotoesScrollView               *photoesV;          //选中的图片的view
@property (nonatomic, strong) UIView                          *videoView;
@property(nonatomic,  strong)EventDealList *listModel;
@property(nonatomic)NSInteger code;
@property(nonatomic,strong)NSMutableArray *loadImageArr;
@property(nonatomic)int beginPirNo;
@property(nonatomic)int endPirNo;
@property(nonatomic)int beginPirDistance;
@property(nonatomic)int endPirDistance;
@property (nonatomic) BOOL isHaveVideo;
@property (nonatomic,strong) NSURL *videoUrl;
@property (nonatomic, strong)NSString *currentUID;//当前道路UID或者是收费站的UID
@property (nonatomic, copy)NSString *directionStr;//方向的数值（1, 2, 3）
@property (nonatomic, strong) HighwayMergeRoadRecordModel *currentHighModel;//当前的线路（主线、南、北）
@property (nonatomic, assign) CGFloat la;
@property (nonatomic, assign) CGFloat lo;
@property (nonatomic, strong) NSMutableArray *selectPhoteArray;
@property(nonatomic,strong)  DateViewController *dateVC;//时间VC
@property (nonatomic, strong) ChooseLineTableViewController *chooseLineVC;
@property (nonatomic, strong) CityDirectionController *citypop;
@property (nonatomic, strong) StationListPopViewController *station;
@property (nonatomic, strong) RampViewController *rampVC;
@property (nonatomic, assign) NSInteger isFirstTime;
@property (nonatomic, strong) NSDate *lastScrollDate;// 上一次滚动的时间
//@property(nonatomic)int beginPirNo;
//@property(nonatomic)int endPirNo;
@end

@implementation ConstructionEventReportViewController

#pragma  --------  收费事件上报施工事件上报

- (UIScrollView *)scroollView {
    if (!_scroollView) {
        _scroollView = [UIScrollView new];
        _scroollView.delegate = self;
        _scroollView.bounces  = NO;
        [self.view addSubview:_scroollView];
        _scroollView.showsVerticalScrollIndicator = YES;
        _scroollView.showsHorizontalScrollIndicator = NO;
        _scroollView.sd_layout
        .topSpaceToView(self.view, 0)
        .leftSpaceToView(self.view, 0)
        .rightSpaceToView(self.view, 0)
        .bottomSpaceToView(self.view, 0);
    }
    return _scroollView;
}
- (PhotoesScrollView *)photoesV {
    if (!_photoesV) {
        PhotoesScrollView *photoesV = [[PhotoesScrollView alloc] init];
        _photoesV = photoesV;
        [self.containerV addSubview:_photoesV];
        self.photoesV.sd_layout
        .widthIs([UIScreen mainScreen].bounds.size.width - 16)
        .heightIs(100)
        .topSpaceToView(self.descripV, 10)
        .centerXEqualToView(self.containerV)
        ;
    }
    return _photoesV;
}
- (UIView *)videoView {
    if (!_videoView) {
        UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width - 20, 200)];
        _videoView = newView;
        //        newView.backgroundColor = [UIColor yellowColor];
        [self.containerV addSubview:_videoView];
        
    }
    return _videoView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpNavigationBar];
    _isFirstTime = 0;
    _listModel = [[EventDealList alloc] init];
    [self setUpUI];
    [self setUpArray];
}
- (void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void) setUpNavigationBar {
    UILabel *label                = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 30)];
    label.text                    = @"施工事件上报";
    label.textAlignment           = NSTextAlignmentCenter;
    label.textColor               = [UIColor whiteColor];
    self.navigationItem.titleView = label;
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"提 交" style:UIBarButtonItemStylePlain target:self action:@selector(report)];
}
//提交判断
- (void)report {
    
    if (self.constructUnitV.desTextV.text.length == 0) {
        [SVProgressHUD showSuccessWithStatus:@"施工单位不能为空"];
        return;
    }
    
    
    if (self.constructUnitV.desTextV.text.length > 50) {
        [SVProgressHUD showSuccessWithStatus:@"施工单位长度不能大于50个字"];
        return;
    }
    
    if (self.personChargeV.desTextV.text.length == 0) {
        [SVProgressHUD showSuccessWithStatus:@"现场负责人不能为空"];
        return;
    }
    
    if (self.personChargeV.desTextV.text.length > 5) {
        [SVProgressHUD showSuccessWithStatus:@"现场负责人不能超过5个字"];
        return;
    }
    
    if (self.contacterV.desTextV.text.length == 0) {
        [SVProgressHUD showSuccessWithStatus:@"联系电话不能为空"];
        return;
    }
    if (self.contacterV.desTextV.text.length > 12) {
        [SVProgressHUD showSuccessWithStatus:@"请输入正确的联系电话"];
        return;
    }
    if (self.beginPileV.midTextField.text.length ==0) {
        [SVProgressHUD showSuccessWithStatus:@"开始桩号不能为空"];
        return;
    }
    
    AppDelegate *myDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    NSMutableArray *pileArray = [[NSMutableArray alloc] init];
    for (PileInfo *pileModel in myDelegate.PirArray) {
        
        if ([_currentHighModel.RoadUID isEqualToString:pileModel.RoadUID]) {
            [pileArray addObject:pileModel];
        }
    }
    NSInteger arrayCount = pileArray.count;
    NSString *midStrB = self.beginPileV.midTextField.text;
    NSString *rightStrB   = self.beginPileV.rightTextField.text;
    NSString *midStrE   = self.endPileV.midTextField.text;
    NSString *rightStrE = self.endPileV.rightTextField.text;
    
    if ([self isNumber:midStrB]) {
        [SVProgressHUD showSuccessWithStatus:@"开始桩号必须为数字"];
        return;
    }
    
    if ([self isNumber:midStrE]) {
        [SVProgressHUD showSuccessWithStatus:@"结束桩号必须为数字"];
        return;
    }
    
    if ([self isNumber:rightStrB]) {
        [SVProgressHUD showSuccessWithStatus:@"开始桩号距离必须为数字"];
        return;
    }
    if ([self isNumber:rightStrE]) {
        [SVProgressHUD showSuccessWithStatus:@"结束桩号距离必须为数字"];
        return;
    }
    
    if ([midStrB integerValue] > pileArray.count) {
        [SVProgressHUD showSuccessWithStatus:[NSString stringWithFormat:@"开始桩号不能大于%ld",(long)arrayCount]];
        return;
    }
    
    if ([midStrB integerValue] > [midStrE integerValue]) {
        [SVProgressHUD showSuccessWithStatus:[NSString stringWithFormat:@"开始桩号不能大于结束桩号"]];
        return;
    }
    
    if (rightStrB.length == 0) {
        [SVProgressHUD showSuccessWithStatus:@"开始桩号距离不能为空"];
        return;
    }
    if (rightStrE.length == 0) {
        [SVProgressHUD showSuccessWithStatus:@"结束桩号距离不能为空"];
        return;
    }
    if (midStrB.length == 0) {
        [SVProgressHUD showSuccessWithStatus:@"开始桩号不能为空"];
        return;
    }
    if (midStrE.length == 0) {
        [SVProgressHUD showSuccessWithStatus:@"结束桩号距离不能为空"];
        return;
    }
    if ([midStrE integerValue] > pileArray.count) {
        [SVProgressHUD showSuccessWithStatus:@"结束桩号距离不能为空"];
        return;
    }
    
    
    //    NSString *midStr   = self.constructPositionV.middelLabel.text;
    //    NSString *rightStr = self.beginPileV.midTextField.text;
    //    NSString *disStr   = self.beginPileV.rightTextField.text;
    //
    //    if (([midStr isEqualToString:@"主线"]) && ([rightStr integerValue] > 269) ) {
    //        [SVProgressHUD showSuccessWithStatus:@"主线开始桩号不能大于269"];
    //        return;
    //    }
    //    if (([midStr isEqualToString:@"韶赣北连线"]) && ([rightStr integerValue]>32)) {
    //        [SVProgressHUD showSuccessWithStatus:@"韶赣北连线开始桩号不能大于32"];
    //        return;
    //    }
    //    //@"韶赣南连线"
    //    if (([midStr isEqualToString:@"韶赣南连线"]) && ([rightStr integerValue]>3)) {
    //        [SVProgressHUD showSuccessWithStatus:@"韶赣南连线开始桩号不能大于3"];
    //        return;
    //    }
    //    if (rightStr) {
    //        [SVProgressHUD showSuccessWithStatus:@"开始桩距离不能为空"];
    //        return;
    //    }
    //    if ([self.beginPileV.rightTextField.text intValue]>999) {
    //        [SVProgressHUD showSuccessWithStatus:@"桩距离不能大于999"];
    //        return;
    //    }
    //    if (self.endPileV.midTextField.text.length == 0 ) {
    //        [SVProgressHUD showSuccessWithStatus:@"结束桩号不能为空"];
    //        return;
    //    }
    //    NSString *endStr = self.endPileV.midTextField.text;
    //    if ([midStr isEqualToString:@"主线"]&&([endStr integerValue]> 269)) {
    //        [SVProgressHUD showSuccessWithStatus:@"主线结束桩号不能大于269"];
    //        return;
    //    }
    //    if ([midStr isEqualToString:@"韶赣北连线"]&&([endStr integerValue]>32)) {
    //        [SVProgressHUD showSuccessWithStatus:@"韶赣北连线结束桩号不能大于32"];
    //        return;
    //    }
    //    if ([midStr isEqualToString:@"韶赣南连线"]&&[endStr integerValue]>3) {
    //        [SVProgressHUD showSuccessWithStatus:@"韶赣南连线结束桩号不能大于3"];
    //        return;
    //    }
    //
    //    if (self.endPileV.rightTextField.text.length == 0) {
    //        [SVProgressHUD showSuccessWithStatus:@"结束桩距离不能为空"];
    //        return;
    //    }
    //    if ([self.endPileV.rightTextField.text integerValue]  > 999) {
    //        [SVProgressHUD showSuccessWithStatus:@"桩距离不能大于999"];
    //        return;
    //    }
    //
    //    if (self.directionV.directionLabel.text.length == 0) {
    //        [SVProgressHUD showSuccessWithStatus:@"请选择方向"];
    //        return;
    //    }
    //    if (self.beginTimeV.timeChooseBtn.currentTitle.length == 0) {
    //        [SVProgressHUD showSuccessWithStatus:@"请选择开始时间"];
    //        return;
    //    }
    //    if (self.endTimeV.timeChooseBtn.currentTitle.length == 0) {
    //        [SVProgressHUD showSuccessWithStatus:@"请选择结束时间"];
    //        return;
    //    }
    NSDate *beginDate = [RODateHelper dateFromString:self.beginTimeV.timeChooseBtn.currentTitle withFormat:nil];
    NSInteger beginInter = [beginDate compareToCurrentDate];
    NSDate *endDate = [RODateHelper dateFromString:self.endTimeV.timeChooseBtn.currentTitle withFormat:nil];
    if (beginInter != 1) {
        [SVProgressHUD showSuccessWithStatus:@"开始时间不能小于当前时间"];
        return;
    }
    
    NSInteger twoDateCompare = [endDate compareToToDate:beginDate];
    if (twoDateCompare != 1 ) {
        [SVProgressHUD showSuccessWithStatus:@"结束时间不能小于开始时间"];
        return;
    }
    
    if (self.projectV.desTextV.text.length == 0) {
        [SVProgressHUD showSuccessWithStatus:@"施工项目不能为空"];
        return;
    }
    if (self.descripV.desTextV.text.length == 0) {
        [SVProgressHUD showSuccessWithStatus:@"施工处理不能为空"];
        return;
    }
    
    if (self.descripV.desTextV.text.length > 1000) {
        [SVProgressHUD showSuccessWithStatus:@"施工处理不能超过1000字"];
        return;
    }
    
    if ([self.descripV.desTextV.text isEqualToString:@"请输入事件描述："])
    {
        [SVProgressHUD showSuccessWithStatus:@"请输入事件描述"];
        return;
    }
    //    if ([self isNumber:self.beginPileV.midTextField.text]) {
    //        [SVProgressHUD showSuccessWithStatus:@"请输入整形数字开始桩号"];
    //        return;
    //    } else {
    //     _beginPirNo=[self.beginPileV.midTextField.text intValue];
    //    }
    //    if ([self isNumber:self.endPileV.midTextField.text]) {
    //        [SVProgressHUD showSuccessWithStatus:@"请输入整形数字结束桩号"];
    //        return;
    //    } else {
    //        _endPirNo=[self.endPileV.midTextField.text intValue];
    //
    //    }
    //    if ([self isNumber:self.beginPileV.rightTextField.text]) {
    //        [SVProgressHUD showSuccessWithStatus:@"请输入整形数字开始桩距离"];
    //        return;
    //    }
    //    else {
    //        _beginPirDistance=[self.beginPileV.rightTextField.text intValue];
    //
    //    }
    //    if ([self isNumber:self.endPileV.rightTextField.text]) {
    //        [SVProgressHUD showSuccessWithStatus:@"请输入整形数字结束桩距离"];
    //        return;
    //    }
    //    else {
    //        _endPirDistance=[self.endPileV.rightTextField.text intValue];
    //
    //    }
    //    if ([self.beginPileV.midTextField.text integerValue]>[self.endPileV.midTextField.text integerValue]) {
    //        [SVProgressHUD showSuccessWithStatus:@"起始桩号不能大于结束桩号"];
    //        return;
    //    }
    //    if (_beginPirNo==_endPirNo) {
    //        if (_beginPirDistance>_endPirDistance) {
    //            [SVProgressHUD showSuccessWithStatus:@"开始装桩距离不能大于结束桩距离"];
    //            return;
    //        }
    //    }
    //    if ([self.beginPileV.midTextField.text intValue]>=999) {
    //        [SVProgressHUD showSuccessWithStatus:@"开始桩距离不能大于999"];
    //        return;
    //    }
    //    if ([self.beginPileV.rightTextField.text intValue]>=999) {
    //        [SVProgressHUD showSuccessWithStatus:@"结束桩距离不能大于999"];
    //        return;
    //    }
    if (!validatePhone(self.contacterV.desTextV.text)) {
        [SVProgressHUD showSuccessWithStatus:@"请输入有效的电话号码"];
        return;
    }
    
    [self requestC];
}

//判断是否是合法的电话号码
- (BOOL) isNumber:(NSString *)text;
{
    NSString *regex =@"[0-9]*";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    if (![pred evaluateWithObject:text]) {
//        NSLog(@"不是");
        return YES;
    }
//    NSLog(@"是");
    return NO;
}

//上报事件
- (void)requestC{
    [SVProgressHUD showWithStatus:@"正在上报"];
    RequestGinsengModel *getCurrentEventModel=[[RequestGinsengModel alloc] init];
    getCurrentEventModel.URL=GetEventDataURL;
    getCurrentEventModel.DataValue = [self dataValueJson];
    getCurrentEventModel.UpdatedTime=@"";
    getCurrentEventModel.SystemCode=@"GuangleHighway";
    getCurrentEventModel.MethodName=@"ReportConstructionEventDetailInfo";
    
    NSDictionary *dic=[[NSDictionary alloc] init];
    dic = @{@"methodName":getCurrentEventModel.MethodName,@"updatedtime":getCurrentEventModel.UpdatedTime,@"systemcode":DefaultSystemCode,@"DataValue":getCurrentEventModel.DataValue};
    [BCNetAPIClient PostVideosAndImages:GetEventDataURL parameters:dic imageArray:_selectPhoteArray videoArray:_videoUrl.absoluteString success:^(id responseObjecet) {
        [SVProgressHUD showSuccessWithStatus:@"成功"];
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^(NSError *error) {
        [SVProgressHUD showSuccessWithStatus:@"失败"];
        [self.navigationController popViewControllerAnimated:YES];
    }];
}
-(NSString *)dataValueJson {
//    AppDelegate *myDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    ConstructionEventDetailInfo *info=[[ConstructionEventDetailInfo alloc] init];
    info.Content=self.projectV.desTextV.text;
    info.EventBaseInfo=[[EventBaseInfo alloc] init];
    
    //    NSArray *arr=[[TableOperation shareInstance]  getePirNoTableOfRow:[@"K" stringByAppendingString:self.BeginPileNO.text]];
    //    for (PileInfo *pir in arr) {
    ////        NSLog(@"%f,%f",pir.Latitude,pir.Longitude);
    //        info.EventBaseInfo.Latitude=pir.Latitude;
    //        info.EventBaseInfo.Longitude=pir.Longitude;
    //    }
    //    AppDelegate *myDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    
    info.Department = self.constructUnitV.desTextV.text;
    info.Header=self.personChargeV.desTextV.text;
    info.Telephone=self.contacterV.desTextV.text;
    
    NSString *k=@"K";
    info.EventBaseInfo.BeginPile=[k stringByAppendingString:self.beginPileV.midTextField.text];
    info.EventBaseInfo.BeginPileDistance=[self.beginPileV.rightTextField.text integerValue];
    info.EventBaseInfo.EndPile=[k stringByAppendingString:self.endPileV.midTextField.text];
    info.EventBaseInfo.EndPileDistance=[self.endPileV.rightTextField.text integerValue];
    info.EventBaseInfo.IncidentSource=@"2";
    
    if ([self.constructPositionV.middelLabel.text isEqualToString:@"收费站"]) {
        info.EventBaseInfo.TollStationUID   = _currentUID;
        info.EventBaseInfo.TollStationName  = self.constructPositionV.rightLabel.text;
        info.EventBaseInfo.RoadUID          = _currentHighModel.RoadUID;
        
        info.EventBaseInfo.RoadDerectionName= self.directionV.directionLabel.text;
        info.EventBaseInfo.Latitude         = _la;
        info.EventBaseInfo.Longitude        = _lo;
        //        NSLog(@"%@ %@ %@ %@",_RampOrNo.text, _Direction.text, _currentUID, _currentHighModel.RoadUID);
    }else {
        info.EventBaseInfo.RoadUID           = _currentUID;
       
        info.EventBaseInfo.RoadDerectionName = self.directionV.directionLabel.text;
        NSString *rampStr = self.constructPositionV.rightLabel.text;
        if ([rampStr isEqualToString:@"主线"]) {
            info.EventBaseInfo.IsRamp = NO;
        }else {
            info.EventBaseInfo.IsRamp = YES;

        }
        AppDelegate *myDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
        for (PileInfo *modelPile in myDelegate.PirArray) {
        //    NSLog(@"%@ %@ %@ %@",modelPile.PileNo, modelPile.RoadUID , info.EventBaseInfo.RoadUID, info.EventBaseInfo.BeginPile);
            NSString *infoRoad = info.EventBaseInfo.RoadUID;
            NSString *infoPile = info.EventBaseInfo.BeginPile;
            NSString *pileRoad = modelPile.RoadUID;
            NSString *pileNO   = modelPile.PileNo;
            if ([infoRoad isEqualToString:pileRoad] && [infoPile isEqualToString:pileNO] ) {
                info.EventBaseInfo.Latitude  = modelPile.Latitude;
                info.EventBaseInfo.Longitude = modelPile.Longitude;
            }
        }
    }
   // NSString *directionNum = [_currentHighModel.highWayRoadDirectionList indexOfObject:info.EventBaseInfo.RoadDerectionName];
    for (HighWayRoadRecordDirectionInfoModel *model in _currentHighModel.highWayRoadDirectionList) {
        if ([model.DirectionDescription isEqualToString:info.EventBaseInfo.RoadDerectionName]) {
             info.EventBaseInfo.RoadDerection     = model.RoadDirection;
        }
    }
    
   
    info.EventBaseInfo.OccurTime          = self.beginTimeV.timeChooseBtn.currentTitle;
    info.EventBaseInfo.EndTime            = self.endTimeV.timeChooseBtn.currentTitle;
    info.EventBaseInfo.IncidentSource     = [NSString stringWithFormat:@"%d",2];
    info.EventBaseInfo.IncidentType       = @"3";
    info.EventBaseInfo.IncidentParentType = @"3 ";
    info.EventBaseInfo.IncidentDetail     = self.projectV.desTextV.text;
    info.EventBaseInfo.DealDetail         = self.descripV.desTextV.text;
    _listModel.DealDetail                 = self.descripV.desTextV.text;
    
    info.EventDealList =@[_listModel];
    
    NSMutableDictionary *dic=info.mj_keyValues;
    NSString *json = [dic mj_JSONString];
    NSLog(@"%@",json);
    
    ParModel *model=[[ParModel alloc] init];
    model.key=@"ConstructionEventDetailInfo";
    model.value=json;
    
    NSMutableDictionary *ModelDic=model.mj_keyValues;
    NSString *dataValue = [ModelDic mj_JSONString];
    NSString *str=@"[";
    NSString *temp=[str stringByAppendingString:dataValue];
    NSString *str1=@"]";
    NSString *JsonString=[temp stringByAppendingString:str1];
    
    return JsonString;
}


- (void) setUpUI {
    self.view.backgroundColor = [UIColor colorWithRed:226.0 / 255 green:230.0 / 255 blue:240.0 / 255 alpha:1];
    self.wrapperView = [UIView new];
    self.wrapperView.backgroundColor = [UIColor colorWithRed:239.0 / 255 green:239.0 / 255 blue:249.0 / 255 alpha:0.8];
    [self.scroollView addSubview:self.wrapperView];
    [self setUpContainerV];
    [self setUpConstructUnitV];
    [self setUpPersonChargeV];
    [self setUpContacterV];
    [self setUpBeginTimeV];
    [self setUpEndTimeV];
    [self setUpConstructPosition];
    [self setUpBeginPileV];
    [self setUpEndPileV];
    [self setUpDirectionV];
    [self setUPProjectV];
    [self setUpDescripV];
    [self setUpPicAndVideoV];
    
    self.wrapperView.sd_layout
    .leftEqualToView(self.scroollView)
    .rightEqualToView(self.scroollView)
    .topEqualToView(self.scroollView)
    ;
    [self.containerV setupAutoHeightWithBottomView:self.descripV bottomMargin:10]   ;
    [self.wrapperView setupAutoHeightWithBottomView:self.picAndVideoV bottomMargin:10]   ;
    [self.scroollView setupAutoContentSizeWithBottomView:self.wrapperView bottomMargin:0];
}

- (void) setUpArray {
    _selectPhoteArray = [[NSMutableArray alloc] init];
}

#pragma  设置UI界面
- (void) setUpContainerV {
    self.containerV = [[UIView alloc] init];
    [self.wrapperView addSubview:self.containerV];
    self.containerV.sd_layout
    .leftSpaceToView(self.wrapperView, 0)
    .rightSpaceToView(self.wrapperView, 0)
    .topSpaceToView(self.wrapperView, 0)
    .autoHeightRatio(0)
    ;
}

- (void) setUpConstructUnitV {
    self.constructUnitV = [[UnStartEventView alloc]initWithDefalutHeight:50.0 title:@"施 工 单 位:"];
    [self.containerV addSubview:self.constructUnitV];
    self.constructUnitV.sd_layout
    .leftSpaceToView(self.containerV, 0)
    .rightSpaceToView(self.containerV, 0)
    .topSpaceToView(self.containerV, 0)
    .autoHeightRatio(0)
    ;
}

- (void) setUpPersonChargeV {
    self.personChargeV = [[UnStartEventView alloc]initWithDefalutHeight:50.0 title:@"现场负责人:"];
    [self.containerV addSubview:self.personChargeV];
    self.personChargeV.sd_layout
    .leftSpaceToView(self.containerV, 0)
    .rightSpaceToView(self.containerV, 0)
    .topSpaceToView(self.constructUnitV, 0)
    .autoHeightRatio(0)
    ;
}

- (void) setUpContacterV {
    self.contacterV = [[UnStartEventView alloc]initWithDefalutHeight:50.0 title:@"联 系 电 话:"];
    self.contacterV.desTextV.keyboardType = UIKeyboardTypePhonePad;
    [self.containerV addSubview:self.contacterV];
    self.contacterV.sd_layout
    .leftSpaceToView(self.containerV, 0)
    .rightSpaceToView(self.containerV, 0)
    .topSpaceToView(self.personChargeV, 0)
    .autoHeightRatio(0)
    ;
}
- (void) setUpBeginTimeV {
    self.beginTimeV = [[ChooseTimeView alloc]initWithDefalutHeight:50.0 title:@"计划开始时间:"];
    [self.containerV addSubview:self.beginTimeV];
    [self.beginTimeV.timeChooseBtn addTarget:self action:@selector(planBeginTimeMethod:) forControlEvents:UIControlEventTouchUpInside];
    self.beginTimeV.sd_layout
    .leftSpaceToView(self.containerV, 0)
    .rightSpaceToView(self.containerV, 0)
    .topSpaceToView(self.contacterV, 0)
    .autoHeightRatio(0)
    ;
}
- (void) setUpEndTimeV {
    self.endTimeV = [[ChooseTimeView alloc]initWithDefalutHeight:40.0 title:@"计划结束时间:"];
    [self.containerV addSubview:self.endTimeV];
    [self.endTimeV.timeChooseBtn addTarget:self action:@selector(planEndTimeMethod:) forControlEvents:UIControlEventTouchUpInside];
    self.endTimeV.sd_layout
    .leftSpaceToView(self.containerV, 0)
    .rightSpaceToView(self.containerV, 0)
    .topSpaceToView(self.beginTimeV, 0)
    .autoHeightRatio(0)
    ;
}
- (void) setUpConstructPosition {
    self.constructPositionV = [[InProgressConstructPositionView alloc] initWithDefalutHeight:50.0 title:@"施 工 地 点:" isCanTouch:YES];
    [self.constructPositionV.middelBtn addTarget:self action:@selector(positionMidBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.constructPositionV.rightBtn addTarget:self action:@selector(positionRightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.containerV addSubview:self.constructPositionV];
    self.constructPositionV.sd_layout
    .leftSpaceToView(self.containerV, 0)
    .rightSpaceToView(self.containerV, 0)
    .topSpaceToView(self.endTimeV, 0)
    .autoHeightRatio(0)
    ;
}
- (void) setUpBeginPileV {
    self.beginPileV = [[ChoosePileNOView alloc] initWithDefalutHeight:50.0 titleString:@"开 始 桩 号:"];
    self.beginPileV.midTextField.keyboardType = UIKeyboardTypePhonePad;
    self.beginPileV.rightTextField.keyboardType = UIKeyboardTypePhonePad;
    [self.containerV addSubview:self.beginPileV];
    self.beginPileV.sd_layout
    .leftSpaceToView(self.containerV, 0)
    .rightSpaceToView(self.containerV, 0)
    .topSpaceToView(self.constructPositionV, 0)
    .autoHeightRatio(0)
    ;
}

- (void) setUpEndPileV {
    self.endPileV = [[ChoosePileNOView alloc] initWithDefalutHeight:50.0 titleString:@"结 束 桩 号:"];
    self.endPileV.midTextField.keyboardType = UIKeyboardTypePhonePad;
    self.endPileV.rightTextField.keyboardType = UIKeyboardTypePhonePad;
    [self.containerV addSubview:self.endPileV];
    self.endPileV.sd_layout
    .leftSpaceToView(self.containerV, 0)
    .rightSpaceToView(self.containerV, 0)
    .topSpaceToView(self.beginPileV, 0)
    .autoHeightRatio(0)
    ;
}

- (void) setUpDirectionV {
    self.directionV = [[ChooseDirectionView alloc] initWithDefalutHeight:50.0 title:@"行 车 方 向:"];
    [self.directionV.chooseDirectBtn addTarget:self action:@selector(popCityDir:)forControlEvents:UIControlEventTouchUpInside];
    [self.containerV addSubview:self.directionV];
    self.directionV.sd_layout
    .leftSpaceToView(self.containerV, 0)
    .rightSpaceToView(self.containerV, 0)
    .topSpaceToView(self.endPileV, 0)
    .autoHeightRatio(0)
    ;
}

- (void) setUPProjectV {
    self.projectV = [[UnStartEventView alloc] initWithDefalutHeight:50.0 title:@"施 工 项 目:"];
    [self.containerV addSubview:self.projectV];
    self.projectV.sd_layout
    .leftSpaceToView(self.containerV, 0)
    .rightSpaceToView(self.containerV, 0)
    .topSpaceToView(self.directionV, 0)
    .autoHeightRatio(0)
    ;
}

- (void)setUpDescripV {
    self.descripV = [[UnStartEventView alloc] initWithDefalutHeight:50.0 title:@"施 工 描 述:"];
    [self.containerV addSubview:self.descripV];
    self.descripV.sd_layout
    .leftSpaceToView(self.containerV, 0)
    .rightSpaceToView(self.containerV, 0)
    .topSpaceToView(self.projectV, 0)
    .autoHeightRatio(0)
    ;
}

- (void) setUpPicAndVideoV {
    self.picAndVideoV = [[InProgressPicAndVideoView alloc] initWithDefalutHeight:40.0];
    [self.wrapperView addSubview:self.picAndVideoV];
    [self.picAndVideoV.picturesBtn addTarget:self action:@selector(photoesBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.picAndVideoV.videoBtn addTarget:self action:@selector(videoBtnClick) forControlEvents:UIControlEventTouchUpInside];
    self.picAndVideoV.sd_layout
    .leftSpaceToView(self.wrapperView, 0)
    .rightSpaceToView(self.wrapperView, 0)
    .topSpaceToView(self.containerV, 10)
    .autoHeightRatio(0)
    ;
}
-(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
    return UIModalPresentationNone;
}
#pragma mark -  图片和视频按钮点击
- (void) videoBtnClick {
    LittleVideoViewController *vc = [[LittleVideoViewController alloc] init];
    vc.delegate = self;
    [self.navigationController presentViewController:vc animated:YES completion:nil];
}

- (void) photoesBtnClick {
    if (self.selectPhoteArray.count >= 5) {
        [SVProgressHUD showErrorWithStatus:@"不能选择超过5张图"];
        return;
    }
    AJPhotoPickerViewController *picker = [[AJPhotoPickerViewController alloc] init];
    picker.maximumNumberOfSelection = 5 - self.selectPhoteArray.count;
    picker.multipleSelection        = YES;
    picker.assetsFilter             = [ALAssetsFilter allPhotos];
    picker.showEmptyGroups          = YES;
    picker.delegate                 = self;
    picker.selectionFilter          = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
        return YES;
    }];
    
    [self presentViewController:picker animated:YES completion:nil];
}

#pragma mark -按钮点击
- (void) positionMidBtnClick:(UIButton *)sender {
    _chooseLineVC = [[ChooseLineTableViewController alloc] init];
    _chooseLineVC.modalPresentationStyle                                 = UIModalPresentationPopover;
    _chooseLineVC.popoverPresentationController.sourceView               = sender;
    _chooseLineVC.popoverPresentationController.delegate                 = self;
    _chooseLineVC.popoverPresentationController.sourceRect               = CGRectMake(70,109,0, 0);
    _chooseLineVC.popoverPresentationController.permittedArrowDirections = NO;
    [self presentViewController:_chooseLineVC animated:YES completion:nil];
    __weak typeof(self) weakSelf=self;
    _chooseLineVC.LineBlock = ^(NSString *lineStr, NSString *uidStr, NSInteger indexRow){
        weakSelf.constructPositionV.middelLabel.text = lineStr;
        if (![uidStr isEqualToString:@""]) {
            _currentUID = uidStr;
            AppDelegate *myDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
            _currentHighModel = myDelegate.HighWayRoadArray[indexRow]; //获取当前道路的模型
            weakSelf.beginPileV.midTextField.text = @"";
            weakSelf.endPileV.midTextField.text   = @"";
            weakSelf.beginPileV.rightTextField.text = @"";
            weakSelf.endPileV.rightTextField.text   = @"";
            [weakSelf getDefaultDeirectionArray];
            [weakSelf configRampOrNo];
            weakSelf.beginPileV.midTextField.enabled  = YES;
            weakSelf.beginPileV.rightTextField.enabled = YES;
            weakSelf.endPileV.midTextField.enabled = YES;
            weakSelf.endPileV.rightTextField.enabled = YES;
            [weakSelf configLabel];
            
        }else  {
            _currentUID = nil;
            weakSelf.beginPileV.midTextField.text = @"";
            weakSelf.endPileV.midTextField.text   = @"";
            weakSelf.beginPileV.rightTextField.text = @"";
            weakSelf.endPileV.rightTextField.text   = @"";
            [weakSelf configStation];
            [weakSelf configLabel];
        }
    };
}
- (void) getDefaultDeirectionArray {
    NSMutableArray *directionArray = [[NSMutableArray alloc] init];
    for (HighWayRoadRecordDirectionInfoModel *directionModel in _currentHighModel.highWayRoadDirectionList) {
        [directionArray addObject:directionModel.DirectionDescription];//行车方向描述
    }
    self.directionV.directionLabel.text = directionArray[0];
    _directionStr = [NSString stringWithFormat:@"%d",1];
}
- (void) configRampOrNo {
    self.constructPositionV.rightLabel.text = @"主线";
}

//桩号
- (void)configLabel {
    
    if ([_currentHighModel.RoadUID isEqualToString:@"8dd13d88-cb02-4471-876b-1dfe39419ee6"]) {
        UILabel *label1 = [[UILabel alloc] init];
        label1.text = @"K";
        label1.frame = CGRectMake(0, 5, 10, 40);
        self.beginPileV.midTextField.leftView = label1;
        self.beginPileV.midTextField.leftViewMode = UITextFieldViewModeAlways;
        
        UILabel *label2 = [[UILabel alloc] init];
        label2.text = @"K";
        label2.frame = CGRectMake(0, 5, 10, 40);
        self.endPileV.midTextField.leftView = label2;
        self.endPileV.midTextField.leftViewMode = UITextFieldViewModeAlways;
        
    }else {
        UILabel *label1 = [[UILabel alloc] init];
        label1.text = @"LK";
        label1.font          = [UIFont boldSystemFontOfSize:15];
        label1.frame =  CGRectMake(0, 0, 25, 10);
        self.beginPileV.midTextField.leftView = label1;
        self.beginPileV.midTextField.leftViewMode = UITextFieldViewModeAlways;
        
        UILabel *label2 = [[UILabel alloc] init];
        label2.text = @"LK";
        label2.font  = [UIFont boldSystemFontOfSize:15];
        label2.frame =  CGRectMake(0, 0, 25, 10);
        self.endPileV.midTextField.leftView = label2;
        self.endPileV.midTextField.leftViewMode = UITextFieldViewModeAlways;
    }
}
- (void) configStation {
    AppDelegate *myDelegate = (AppDelegate*) [UIApplication sharedApplication].delegate;
    StationModel *model = myDelegate.AllTollStationArray[0];
    _currentUID = model.TollStationUID;
    _la         = model.Latitude;
    _lo         = model.Longitude;
    for (HighwayMergeRoadRecordModel *highModel in myDelegate.HighWayRoadArray) {
        if ([highModel.RoadUID isEqualToString:model.RoadUID]) {
            _currentHighModel = highModel;
        }
    }
    [self configLabel];
    [self getDefaultDeirectionArray];
    
    self.constructPositionV.rightLabel.text          = model.TollStationName;
    self.beginPileV.midTextField.text       = [model.PileNo substringWithRange:NSMakeRange(1, 1)];
    self.beginPileV.rightTextField.text = [NSString stringWithFormat:@"%ld",(long)model.PileDistance];
    self.endPileV.midTextField.text         = [model.PileNo substringWithRange:NSMakeRange(1, 1)];
    self.endPileV.rightTextField.text   = [NSString stringWithFormat:@"%ld",(long)model.PileDistance];
    self.beginPileV.midTextField.enabled       = NO;
    self.beginPileV.midTextField.enabled = NO;
    self.endPileV.midTextField.enabled         = NO;
    self.endPileV.rightTextField.enabled   = NO;
}
- (void)popCityDir:(UIButton *)btn {
    _citypop   = [[CityDirectionController alloc] init];
    _citypop.currentModel  = _currentHighModel;
    _citypop.modalPresentationStyle=UIModalPresentationPopover;
    __weak typeof(self) weakSelf=self;
    _citypop.LineBlock=^(NSString *str, NSInteger index){
        weakSelf.directionV.directionLabel.text = str;
       _directionStr = [NSString stringWithFormat:@"%d",(int)index];
        
    };
    _citypop.popoverPresentationController.sourceView=btn;
    _citypop.popoverPresentationController.delegate=self;
    _citypop.popoverPresentationController.sourceRect=CGRectMake(135, 109, 0, 0);
    _citypop.popoverPresentationController.permittedArrowDirections=NO;
    [self presentViewController:_citypop animated:YES completion:nil];
}


- (void) positionRightBtnClick:(UIButton *)sender {
    __weak typeof(self) weakSelf = self;
    AppDelegate *myDelegate = (AppDelegate*) [UIApplication sharedApplication].delegate;
    if ([self.constructPositionV.middelLabel.text isEqualToString: @"收费站"]) {
        _station                          = [[StationListPopViewController alloc] init];
        _station.modalPresentationStyle   = UIModalPresentationPopover;
        _station.popoverPresentationController.sourceView = sender;
        _station.popoverPresentationController.delegate   = self;
        _station.popoverPresentationController.sourceRect = CGRectMake(80, 135, 0,0);
        _station.popoverPresentationController.permittedArrowDirections = NO;
        _station.LineBlock=^(StationModel *model){
            weakSelf.constructPositionV.rightLabel.text = model.TollStationName;
            _la = model.Latitude;
            _lo = model.Longitude;
            _currentUID = model.TollStationUID;
            for (HighwayMergeRoadRecordModel *highModel in myDelegate.HighWayRoadArray) {
                if ([highModel.RoadUID isEqualToString:model.RoadUID]) {
                    _currentHighModel = highModel;
                    [weakSelf configLabel];
                }
            }
            [weakSelf getDefaultDeirectionArray];
            NSInteger length = model.PileNo.length;
            weakSelf.beginPileV.midTextField.text       = [model.PileNo substringWithRange:NSMakeRange(1,  length -1)];
            weakSelf.beginPileV.rightTextField.text = [NSString stringWithFormat:@"%ld",(long)model.PileDistance];
            weakSelf.endPileV.midTextField.text         = [model.PileNo substringWithRange:NSMakeRange(1,  length -1)];
            weakSelf.endPileV.rightTextField.text   = [NSString stringWithFormat:@"%ld",(long)model.PileDistance];
            weakSelf.beginPileV.midTextField.enabled          = NO;
            weakSelf.beginPileV.rightTextField.enabled    = NO;
            weakSelf.endPileV.midTextField.enabled            = NO;
            weakSelf.endPileV.rightTextField.enabled      = NO;
        };
        [self presentViewController:_station animated:YES completion:nil];
    }else {
        _rampVC=[[RampViewController alloc] init];
        _rampVC.modalPresentationStyle=UIModalPresentationPopover;
        _rampVC.RampBlock=^(NSInteger index){
            if (index==0) {
                weakSelf.constructPositionV.rightLabel.text=@"主 线";
            }
            if (index==1) {
                weakSelf.constructPositionV.rightLabel.text=@"匝 道";
            }
        };
        _rampVC.popoverPresentationController.sourceView=sender;
        _rampVC.popoverPresentationController.delegate=self;
        _rampVC.popoverPresentationController.sourceRect=CGRectMake(80, 85, 0, 0);
        _rampVC.popoverPresentationController.permittedArrowDirections=NO;
        [self presentViewController:_rampVC animated:YES completion:nil];
    }
}

- (void) setUpDatePicker:(UIButton *)sender {
    
    EKDateTimePickerView *datepicker = [[EKDateTimePickerView alloc]initWithDatePosition:EKDatePositionMiddel CompleteBlock:^(NSDate *startDate) {
        _lastScrollDate = startDate;
        NSString *dateStr = [startDate stringWithFormat:@"yyyy-MM-dd HH:mm:ss" ];
        [sender setTitle:dateStr forState:UIControlStateNormal];
    }];
    datepicker.datePickerStyle = EKDateStyleShowYearMonthDayHourMinute;
    datepicker.themeColor      = [UIColor colorWithHexString:@"38b1dc"];
    if (sender.currentTitle != nil) {
        _lastScrollDate = nil;
        [datepicker scrollToSepicefyDate:[RODateHelper dateWithOutTimeZoneFromString:sender.currentTitle withFormat:nil] animated:NO];
    }
    
    if (_lastScrollDate != nil) {
        [datepicker scrollToSepicefyDate:_lastScrollDate animated:NO];
    }

    [datepicker show];
}


- (void)planBeginTimeMethod:(UIButton *)sender {
    [self setUpDatePicker:self.beginTimeV.timeChooseBtn];

}

- (void)planEndTimeMethod:(UIButton *)sender {
    [self setUpDatePicker:self.endTimeV.timeChooseBtn];

}
#pragma mark - BoPhotoPickerProtocol
- (void)photoPickerDidCancel:(AJPhotoPickerViewController *)picker {
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)photoPicker:(AJPhotoPickerViewController *)picker didSelectAssets:(NSArray *)assets {
    if ((self.selectPhoteArray.count + assets.count) > 5)  {
        [SVProgressHUD showSuccessWithStatus:@"不能超过5张"];
        return;
    }
    [picker dismissViewControllerAnimated:NO completion:nil];
    for (int i = 0; i < assets.count; i ++) {
        ALAsset *asset =  assets[i];
        UIImage *tempImg = [UIImage imageWithCGImage:asset.defaultRepresentation.fullScreenImage];
        [self.selectPhoteArray addObject:tempImg];
    }
    _isFirstTime += 1;
    [self reConfigPhotoesView];
}

- (void) reConfigPhotoesView {
    for (UIImageView *imgV in self.photoesV.scrollV.subviews) {
        [imgV removeFromSuperview];
    }
    CGFloat x = 5;
    CGRect frame = CGRectMake(0, 0, 100, 100);
    
    self.photoesV.scrollV.contentSize = CGSizeMake(105 * self.selectPhoteArray.count + 5, 0);
    for (int i = 0 ; i < self.selectPhoteArray.count; i++) {
        frame.origin.x = x;
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
        [imageView setContentMode:UIViewContentModeScaleAspectFill];
        imageView.clipsToBounds          = YES;
        imageView.image                  = self.selectPhoteArray[i];
        imageView.tag                    = i;
        imageView.userInteractionEnabled = YES;
        [imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showBig:)]];
        [self.photoesV.scrollV addSubview:imageView];
        
        x += frame.size.width+5;
    }
    if (_isFirstTime == 1) {
        if (_videoUrl) {
            [self reconfigVideoView];
        }else {
            [self.containerV setupAutoHeightWithBottomView:self.photoesV bottomMargin:10];
            [self.containerV layoutSubviews];
        }
        
    }
}

- (void)showBig:(UITapGestureRecognizer *)sender {
    NSInteger index = sender.view.tag;
    AJPhotoBrowserViewController *photoBrowserViewController = [[AJPhotoBrowserViewController alloc] initWithPhotos:self.selectPhoteArray index:index];
    photoBrowserViewController.delegate = self;
    //    UIImage *img = self.selectPhoteArray[index];
    //    [self.selectPhoteArray removeObjectAtIndex:index];
    _isFirstTime += 1;
    [self presentViewController:photoBrowserViewController animated:YES completion:nil];
}

- (void)photoBrowser:(AJPhotoBrowserViewController *)vc deleteWithIndex:(NSInteger)index {
    [self.selectPhoteArray removeObjectAtIndex:index];
    //    [self.photos removeObjectAtIndex:index];
    //    [self removeDeleteImages];
    [self reConfigPhotoesView];
}

- (void)photoBrowser:(AJPhotoBrowserViewController *)vc didDonePhotos:(NSArray *)photos {
    [vc dismissViewControllerAnimated:YES completion:nil];
    _isFirstTime += 1;
    [self reConfigPhotoesView];
}

- (void)photoPickerTapCameraAction:(AJPhotoPickerViewController *)picker {
    [self checkCameraAvailability:^(BOOL auth) {
        if (!auth) {
            NSLog(@"没有访问相机权限");
            return;
        }
        [picker dismissViewControllerAnimated:NO completion:nil];
        UIImagePickerController *cameraUI = [UIImagePickerController new];
        cameraUI.allowsEditing   = NO;
        cameraUI.delegate        = self;
        cameraUI.sourceType      = UIImagePickerControllerSourceTypeCamera;
        cameraUI.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;
        [self presentViewController: cameraUI animated: YES completion:nil];
    }];
}
- (void)checkCameraAvailability:(void (^)(BOOL auth))block {
    BOOL status = NO;
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if(authStatus == AVAuthorizationStatusAuthorized) {
        status = YES;
    } else if (authStatus == AVAuthorizationStatusDenied) {
        status = NO;
    } else if (authStatus == AVAuthorizationStatusRestricted) {
        status = NO;
    } else if (authStatus == AVAuthorizationStatusNotDetermined) {
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
            if(granted){
                if (block) {
                    block(granted);
                }
            } else {
                if (block) {
                    block(granted);
                }
            }
        }];
        return;
    }
    if (block) {
        block(status);
    }
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    
    NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
    UIImage *originalImage;
    if (CFStringCompare((CFStringRef) mediaType,kUTTypeImage, 0)== kCFCompareEqualTo) {
        originalImage = (UIImage *) [info objectForKey:UIImagePickerControllerOriginalImage];
    }
    
    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
        UIImageWriteToSavedPhotosAlbum(originalImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    }else {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}
//保存图片 
- (void)image:(UIImage*)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo {
    if (!error) {
        //        NSLog(@"保存到相册成功");
        AJPhotoPickerViewController *picker = [[AJPhotoPickerViewController alloc] init];
        picker.maximumNumberOfSelection = 5;
        picker.multipleSelection        = YES;
        picker.assetsFilter             = [ALAssetsFilter allPhotos];
        picker.showEmptyGroups          = YES;
        picker.delegate                 = self;
        picker.selectionFilter          = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
            return YES;
        }];
        [self dismissViewControllerAnimated:NO completion:nil];
        [self presentViewController:picker animated:NO completion:nil];
    }else{
        //        NSLog(@"保存到相册出错%@", error);
    }
}
// *********** - 播放小视频 - *************
#pragma mark -littleVideoDelegate
- (void)finishLittleVideoViewControllerCapture:(NSURL *)filePath {
    _videoUrl = filePath;
    
    [self reconfigVideoView];
    
    [self crreatPlayViedeo:filePath];
}

- (void) reconfigVideoView {
    UIView *view;
    if (self.selectPhoteArray.count > 0) {
        view = self.photoesV;
    }else {
        view = self.descripV;
    }
    if (_videoUrl) {
        self.videoView.sd_layout
        .widthIs([UIScreen mainScreen].bounds.size.width - 16)
        .heightIs(200)
        .topSpaceToView(view, 10)
        .centerXEqualToView(self.containerV)
        ;
        [self.containerV setupAutoHeightWithBottomView:self.videoView bottomMargin:10];
        [self.containerV layoutSubviews];
        [self.wrapperView layoutSubviews];
    }
}

- (void) crreatPlayViedeo:(NSURL *)videoUrl {
    _playItem = [AVPlayerItem playerItemWithURL:videoUrl];
    _player = [AVPlayer playerWithPlayerItem:_playItem];
    _playerLayer =[AVPlayerLayer playerLayerWithPlayer:_player];
    _playerLayer.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width - 16, 200);
    _playerLayer.videoGravity=AVLayerVideoGravityResizeAspectFill;//视频填充模式
    [self.videoView.layer addSublayer:_playerLayer];
    [_player play];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [_player pause];
    _player = nil;
    SendNotify(GLConstructEventReportNotification, self)
}



@end
